<template>
  <div class="mod-demo-ueditor">
    <div>
      <el-tree
      :data="treeData"
      show-checkbox
      node-key="catId"
      ref="tree"
      highlight-current
      :expand-on-click-node="false"
      :default-expanded-keys="nodeKeys"
      :props="defaultProps">
      <span class="custom-tree-node" slot-scope="{ node, data }">
        <span>{{ node.data.name }}</span>
          <span>
            <el-button
              v-if="node.level<3"
              type="text"
              size="mini"
              @click="() => append(data)">
              添加
            </el-button>
             <el-button
              type="text"
              size="mini"
              @click="() => edit(data)">
              编辑
            </el-button>
            <el-button
              v-if="node.childNodes.length==0"
              type="text"
              size="mini"
              @click="() => remove(node, data)">
              删除
            </el-button>
          </span>
        </span>
      </el-tree>
      <el-dialog title="分类信息" :append-to-body="true" :visible.sync="dialogFormVisible">
        <el-form :model="categoryTemp">
          <el-form-item label="分类名称">
            <el-input v-model="categoryTemp.name" autocomplete="off"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="dialogStatus=='create'?addCategory():updateCategory()">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
  
</template>

<script>
  export default {
    data () {
      return {
        treeData: [],
        defaultProps: {
          children: 'children',
          label: 'name'
        },
        nodeKeys:[],
        dialogFormVisible: false,
        categoryTemp:{"showStatus":1,sort:1,"productCount":0},
        dialogStatus:""
      }
    },
    mounted () {
      
      this.getData()
    },
    methods: {
      getData(){
        this.$http({
            url: this.$http.adornUrl('/product/pmscategory/list/tree'),
            method: 'get',
            data: {}
        }).then(({data}) => {
            this.treeData=data.data
        })   
      },

      getCheckedNodes() {
        console.log(this.$refs.tree.getCheckedNodes());
      },
      getCheckedKeys() {
        console.log(this.$refs.tree.getCheckedKeys());
      },
      
      resetChecked() {
        this.$refs.tree.setCheckedKeys([]);
      },
      append(data) {
        this.dialogStatus="create"
        this.dialogFormVisible=true
        this. categoryTemp={"showStatus":1,sort:1,"productCount":0};
        this.categoryTemp.parentCid=data.catId
        this.categoryTemp.catLevel=data.catLevel+1
      },
      edit(data){
        this.dialogStatus="update"
        this.dialogFormVisible=true
        this. categoryTemp=data
      },
      remove(node, data) {
        var catIds=[data.catId]
        this.$confirm('确定删除'+data.name+'吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/product/pmscategory/delete'),
            method: 'post',
            data: this.$http.adornData(catIds,false)
            }).then(({data}) => {
                this.$message({
                  type: 'success',
                  message: '删除成功!'
                });
                this.nodeKeys=[node.parent.data.catId];
                this.getData()
            })   
            
        })
      },

      //添加分类
      addCategory(){
        this.$http({
        url: this.$http.adornUrl('/product/pmscategory/save'),
        method: 'post',
        data: this.$http.adornData(this.categoryTemp,false)
        }).then(({data}) => {
          this.$message({
            type: 'success',
            message: '添加成功!'
          });
          this.nodeKeys=[];
          this.nodeKeys.push(this.categoryTemp.parentCid)
          this.getData()
          this.dialogFormVisible=false
        })   
      },
      updateCategory(){
        this.$http({
        url: this.$http.adornUrl('/product/pmscategory/update'),
        method: 'post',
        data: this.$http.adornData(this.categoryTemp,false)
        }).then(({data}) => {
          this.$message({
            type: 'success',
            message: '编辑成功!'
          });
          this.nodeKeys=[];
          this.nodeKeys.push(this.categoryTemp.catId)
          this.getData()
          this.dialogFormVisible=false
        })   
      }
     
    }
  }
</script>

<style lang="scss">
  .mod-demo-ueditor {
    position: relative;
    z-index: 510;
    > .el-alert {
      margin-bottom: 10px;
    }
  }
</style>